﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Bloodbank.Api;
using Bloodbank.Dto;

namespace Bloodbank
{
    public partial class frmRegister : Form
    {
        public frmRegister()
        {
            InitializeComponent();
        }

        private void LoadCbbGroupBlood()
        {
            BloodGroupApi bloodGroupApi = new BloodGroupApi();
            cbbBloodgroup.DisplayMember = "BloodGroupName";
            cbbBloodgroup.ValueMember = "Id";
            cbbBloodgroup.DataSource = bloodGroupApi.GetGroupBloodList();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmRegister_Load(object sender, EventArgs e)
        {
            radMale.Checked = true;
            LoadCbbGroupBlood();
        }

        private void btnRegister_Click(object sender, EventArgs e)
        {
            if (txtUsername.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Username is required", "Message");
            }
            else if (txtPassword.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Password is required", "Message");
            }
            else if (txtConfirmPassword.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Confirm password is required", "Message");
            }
            else if (txtFullname.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Fullname is required", "Message");
            }
            else if (txtConfirmPassword.Text.Trim() != txtPassword.Text.Trim())
            {
                MessageBox.Show("Password is incorrect", "Message");
            }
            else {
                CheckLoginDto checkLogin = new CheckLoginDto();
                CheckedLoginApi checkedLoginApi = new CheckedLoginApi();
                checkLogin = checkedLoginApi.CheckExistsUser(txtUsername.Text.Trim());
                if (checkLogin.IsSuccess) {
                    MessageBox.Show(checkLogin.Message, "Message");
                    return;
                }
                UserDto user = new UserDto()
                {
                    Username = txtUsername.Text.Trim(),
                    Password = txtPassword.Text.Trim(),
                    ConfirmPassword = txtConfirmPassword.Text.Trim(),
                    Email = txtEmail.Text.Trim(),
                    FullName = txtFullname.Text.Trim(),
                    Address = txtAddress.Text.Trim(),
                    City = txtCity.Text.Trim(),
                    State = txtState.Text.Trim(),
                    Gender = radMale.Checked ? true : false,
                    Phone = txtPhone.Text.Trim(),
                    Birthday = DateTime.Parse(dtpBirthday.Value.ToString()),
                    BloodGroupId = int.Parse(cbbBloodgroup.SelectedValue.ToString())
                };
                UserApi userApi = new UserApi();
                userApi.Inserted(user);
                MessageBox.Show("Account is created successfully", "Message");
                this.Close();                
            }
        }
    }
}
